var o = {};

var link = new Function();
link.prototype = o;

o.showAdd = function() {
	$('<div id="add_link"></div>').appendTo($('body'));
	;
	$("#add_link").load(ROOT + '/link/link-showAdd.action');
	$.ligerDialog.open( {
		target : $("#add_link"),
		height : 180,
		width : 400,
		modal : true,
		buttons : [ {
			text : '确定',
			onclick : function(item, dialog) {
				var form = $('#addform');
				var title = $('#title').val();
				if (!title) {
					alert("不能为空");
					return;
				}
				var url = $('#url').val();
				if (!url) {
					alert("不能为空");
					return;
				}

				$.ajax( {
					type : "POST",
					url : "link-addLink.action",
					data : form.serialize(),
					success : function(msg) {
						alert(msg.jsonResult);
						dialog.close();
						window.location.reload(false);
					},
					error : function(XMLHttpRequest, textStatus, errorThrown) {
						alert("出错了：" + errorThrown);
					}
				});

			}
		}, {
			text : '取消',
			onclick : function(item, dialog) {
				dialog.close();
			}
		} ]
	});
}

o.editLink = function(id) {
	$('<div id="add_link"></div>').appendTo($('body'));
	;
	$("#add_link").load(ROOT + '/link/link-showAdd.action?id=' + id);
	$.ligerDialog.open( {
		target : $("#add_link"),
		height : 180,
		width : 400,
		modal : true,
		buttons : [ {
			text : '确定',
			onclick : function(item, dialog) {
				var form = $('#addform');
				var title = $('#title').val();
				if (!title) {
					alert("不能为空");
					return;
				}
				var url = $('#url').val();
				if (!url) {
					alert("不能为空");
					return;
				}

				$.ajax( {
					type : "POST",
					url : "link-editLink.action",
					data : form.serialize(),
					success : function(msg) {
						alert(msg.jsonResult);
						dialog.close();
						window.location.reload(false);
					},
					error : function(XMLHttpRequest, textStatus, errorThrown) {
						alert("出错了：" + errorThrown);
					}
				});

			}
		}, {
			text : '取消',
			onclick : function(item, dialog) {
				dialog.close();
			}
		} ]
	});
}

/**
 * 选择
 */
o.checkInAll = function(obj) {
	if (obj.attr('checked') == true) {
		$('#IN .selectItem').attr('checked', 'checked');
	} else {
		$('#IN .selectItem').attr('checked', '');
	}
}
/**
 * 删除
 */
o.delBatch = function(tab) {
	var ids = "";
	$('#' + tab + ' .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if (ids == "") {
		alert("请选择要删除的记录!");
		return;
	}
	$.ligerDialog.confirm('确定删除?', function(yes)
			{
				if(yes){
					$.ajax( {
						type : "POST",
						url : "link-delBatch.action",
						data : "ids=" + ids,
						success : function(msg) {
							window.location.reload(false);
						},
						error : function(XMLHttpRequest, textStatus, errorThrown) {
							alert("出错了：" + errorThrown);
						}
					});
				}
			});
}

o.top = function(id) {
	$.ajax( {
		type : "POST",
		url : ROOT + "/link/link-top.action",
		data : "id=" + id,
		success : function(msg) {
			window.location.reload(false);
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			alert("出错了：" + errorThrown);
		}
	});

};
o.up = function(id) {
	$.ajax( {
		type : "POST",
		url : ROOT + "/link/link-up.action",
		data : "id=" + id,
		success : function(msg) {
			window.location.reload(false);
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			alert("出错了：" + errorThrown);
		}
	});

};
o.down = function(id) {
	$.ajax( {
		type : "POST",
		url : ROOT + "/link/link-down.action",
		data : "id=" + id,
		success : function(msg) {
			window.location.reload(false);
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			alert("出错了：" + errorThrown);
		}
	});

};